package com.meiyue.library.utils;

import com.meiyue.bean.protocol.NetResult;

/**
 * ChrisSpringDemo
 * com.meiyue.library.utils
 * Created by Chris Chen
 * 2017/9/13
 * Explain:构建返回结果的工具
 */
public class ResultUtils {
    /**
     * 构建一个字符串类型的返回结果
     *
     * @param obj
     * @return
     */
    public static String build(Object obj) {
        return JsonUtils.toJson(obj);
    }

    /**
     * 构建一个NetResult<DataType>类型的返回结果
     *
     * @param obj
     * @param <DataType>
     * @return
     */
    public static <DataType> NetResult<DataType> buildResult(DataType obj) {
        NetResult<DataType> result = new NetResult<>();
        if (obj != null) {
            result.setCode(0);
            result.setMsg("success");
            result.setData(obj);
        } else {
            result.setCode(1);
            result.setMsg("error");
            result.setData(null);
        }
        return result;
    }
}
